package com.htg.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class DialogNewVersionHint {

    private Context cxt;

    private AlertDialog alertDialog = null;


    public DialogNewVersionHint(Context cxt) {
        this.cxt = cxt;
    }

    public void show(String versionName, String content, View.OnClickListener onUpdateClickLis, View.OnClickListener onCloseClickLis) {
        if (alertDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(cxt);
            alertDialog = builder.create();
        } else {
            if (alertDialog.isShowing()) {
                return;
            }
        }
        alertDialog.show();
        View view = LayoutInflater.from(cxt).inflate(R.layout.dialog_newversion_hint, null);
        Window window = alertDialog.getWindow();
        window.setBackgroundDrawableResource(R.drawable.shape_alpha);
        WindowManager.LayoutParams params = window.getAttributes();
        int width = cxt.getResources().getDimensionPixelSize(R.dimen.dp_260);

        params.width = width;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        //window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
        window.setContentView(view);

        TextView tvVersionName = view.findViewById(R.id.tv_dialog_version_name);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) TextView tvContent = view.findViewById(R.id.tv_dialog_version_content);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) TextView tvUpdate = view.findViewById(R.id.tv_dialog_version_update);
        @SuppressLint({"MissingInflatedId", "LocalSuppress"}) RelativeLayout rlClose = view.findViewById(R.id.rl_dialog_version_close);

        tvVersionName.setText(versionName);
        if (content != null && !content.equals("") && !content.equals("null"))
            tvContent.setText(content);
        tvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                onUpdateClickLis.onClick(v);
            }
        });
        rlClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                onCloseClickLis.onClick(v);
            }
        });

    }


    public void dismis() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
        alertDialog = null;
    }


}
